package handler

import (
	"github.com/gin-gonic/gin"
	"logi-deliv/model"
	"strconv"
)

func RoleNodes(c *gin.Context) {
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	nid, _ := strconv.Atoi(c.PostForm("nid"))
	data := model.RoleNode{
		Rid: rid,
		Nid: nid,
	}
	model.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
	})
	return

}

func UsersRole(c *gin.Context) {
	/*
		SELECT
		*
		FROM
		nodes LEFT JOIN roles on nodes.rid = roles.id
	*/
	id, _ := strconv.Atoi(c.Query("id"))

	var role []model.Role
	model.DB.Table("roles").
		Joins("LEFT JOIN users on users.rid = roles.id").
		Where("roles.id=?", id).Debug().Find(&role)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": role,
	})
	return
}

func RoleNode(c *gin.Context) {
	id, _ := strconv.Atoi(c.Query("id"))
	var node []model.Node
	model.DB.Table("nodes").
		Joins("LEFT JOIN roles on nodes.rid = roles.id").
		Where("nodes.id=?", id).Debug().Find(&node)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": node,
	})
	return
}
